package thread;

/**
 * Created by Administrator on 2017/10/19 0019.
 */
public class Prictice1
{
    public static void main(String[] args) throws Exception
    {
        Object o1 = new Object();
        Object o2 = new Object();
        T1 t1 = new T1(o1, o2);
        T2 t2 = new T2(o2, o1);
        t1.start();
        Thread.sleep(50);
        t2.start();
    }
    
    static class T1 extends Thread
    {
        
        Object prev, self;
        public T1(Object o1, Object self)
        {
            prev = o1;
            this.self = self;
        }

        @Override
        public void run()
        {
            try
            {
                int printClac = 0;
                for (int i = 0; i < 52; i++)
                {
                    synchronized (prev)
                    {
                        synchronized (self)
                        {
                            System.out.println(i+1);
                            printClac++;
                            if(printClac == 2)
                            {
                                self.notify();
                            }
                        }
                        if(printClac == 2)
                        {
                            printClac = 0;
                            prev.wait();
                        }
                    }
                }
            }catch (Exception e){}
        }
    }

    static class T2 extends Thread
    {
        Object prev, self;
        public T2(Object o1, Object self)
        {
            prev = o1;
            this.self = self;
        }

        @Override
        public void run()
        {
            try
            {
                for (int i = 0; i < 26; i++)
                {
                    synchronized (prev)
                    {
                        synchronized (self)
                        {
                            System.out.println((char)(i + 'A'));
                            self.notify();
                        }
                        prev.wait();
                    }
                }
            }catch (Exception e){}
        }
    }
}
